﻿<%@ WebHandler Language="C#" Class="manage" %>

using System;
using System.Web;
using System.Data;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;
using common;
using common.web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Drawing;
using System.Drawing.Imaging;

public class manage : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        context.Response.AddHeader("Access-Control-Allow-Origin", "*");
        string _t = context.Request["t"];
        context.Response.Write(getData(_t));
    }

    string getData(string t)
    {
        switch (t)
        {
            case "Count":
                return Count();                        //统计
            case "AuditList":
                return AuditList();                    //审核列表
            case "AuditPass":
                return AuditPass();                    //审核通过
            case "BuildDir":
                return BuildDir();                      //新建目录
            case "AuditSearch":
                return AuditSearch();                  //审核检索
            case "SendMessage":
                return SendMessage();                  //发送公告
            case "MessageList":
                return MessageList();                  //公告列表
        }
        return "缺少参数!";
    }

    /// <summary>
    /// 统计
    /// </summary>
    protected string Count()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if (_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        int audit = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filecollection where fApplyforupdate = 1 and fIschecked = 0 and fuid = " + _uid));                   //待审核上传
        int nosale = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filecollection where fApplyforsale = 1 and fIschecked = 0 and fuid = " + _uid));                    //待审核下架
        int nowdown = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 and datediff(day,tdate,getdate())=0 and fuid =" + _uid));           //今日下载
        int nowup = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 and datediff(day,tdate,getdate())=0 and fuid =" + _uid));             //今日上传
        int nowedit = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 and datediff(day,tdate,getdate())=0 and fuid =" + _uid));           //今日修改
        int nownosale = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 and datediff(day,tdate,getdate())=0 and fuid =" + _uid));         //今日下架

        int weekdown = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 and datediff(week,tdate,getdate())=0 and fuid =" + _uid));         //本周下载
        int weekup = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 and datediff(week,tdate,getdate())=0 and fuid =" + _uid));           //本周上传
        int weekedit = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 datediff(week,tdate,getdate())=0 and fuid =" + _uid));             //本周修改
        int weekosale = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 datediff(week,tdate,getdate())=0 and fuid =" + _uid));            //本周下架

        int monthdown = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 and  datediff(month,[tdate],getdate())=0 and fuid =" + _uid));    //本月下载
        int monthup = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 and datediff(month,[tdate],getdate())=0 and fuid =" + _uid));       //本月上传
        int monthedit = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 and datediff(month,[tdate],getdate())=0 and fuid =" + _uid));     //本月修改
        int monthosale = Convert.ToInt32(db.ExecNumb("select count(1) from t_Filesrecording where cType = 1 and datediff(month,[tdate],getdate())=0 and fuid =" + _uid));    //本月下架

        dynamic inv = new JObject();
        inv.audit = audit;
        inv.nosale = nosale;

        inv.nowdown = nowdown;
        inv.nowup = nowup;
        inv.nowedit = nowedit;
        inv.nownosale = nownosale;

        inv.weekdown = weekdown;
        inv.weekup = weekup;
        inv.weekedit = weekedit;
        inv.weekosale = weekosale;

        inv.monthdown = monthdown;
        inv.monthup = monthup;
        inv.monthedit = monthedit;
        inv.monthosale = monthosale;

        ja.Add(inv);

        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 审核列表
    /// </summary>
    protected string AuditList()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if (_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }

        int _page = pgfunc.getPage(HttpContext.Current.Request["page"]);
        int _pagesize = pgfunc.getPage(HttpContext.Current.Request["pagesize"]);
        int rsct, pgct;
        string sql = pgfunc.GetSqlstr5(_page, _pagesize, "b.id,b.cFilename,b.tDate,b.cUser,a.cTypesm,c.cMc,d.cMlmc,a.cTypesm", "t_Filesrecording a left join t_Filecollection b on a.fFileid = b.id left join t_Department c on c.id = b.fDptid left join t_Filecatalogue d on d.id = b.fDptid and a.fuid = " + _uid, " order by a.id desc ", out rsct, out pgct);

        jo.Add("Rsct", rsct);
        jo.Add("Pgct", pgct);

        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 审核通过
    /// </summary>
    protected string AuditPass()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if (_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _fileid = func.safeStr(Req("fileid"));
        if (!string.IsNullOrEmpty(_fileid) && func.chkRegex(_fileid, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "fileid参数错误");
            return JsonConvert.SerializeObject(jo);
        }

        string _shjg = func.safeStr(Req("shjg"));
        if (!string.IsNullOrEmpty(_shjg) && func.chkRegex(_fileid, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "fileid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        if (_shjg != "1") _shjg = "0";

        string _user = db.ExecOne("select cUser from [t_Users] where id = " + _uid);
        db.ExecNQ("insert into [t_Filesrecording] (fuid,cUser,fFileid,cType,cTypesm) values (" + _uid + ",'" + _user + "'," + _fileid + ",3,'审核')");
        db.ExecNQ("update [t_Filecollection] set tShsj = '" + DateTime.Now.ToString() + "',fShjg = " + _shjg + " where id = " + _fileid);

        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 新建目录
    /// </summary>
    protected string BuildDir()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if (_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _mlmc = func.safeStr(Req("mlmc"));
        if (!string.IsNullOrEmpty(_mlmc))
        {
            jo.Add("Result", 2);
            jo.Add("Message", "mlmc参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _dptid = func.safeStr(Req("dptid"));
        if (!string.IsNullOrEmpty(_dptid) && func.chkRegex(_dptid, @"^\d{1,9}(\.\d{1,2})?$") == false)
        {
            jo.Add("Result", 2);
            jo.Add("Message", "dptid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        db.ExecNQ("insert into [t_Filecatalogue] (cMlmc,fUid,fDptid,fIsOk) values ('" + _mlmc + "'," + _uid + "," + _dptid + ",1')");

        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 审核检索
    /// </summary>
    protected string AuditSearch()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if (_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        int _page = pgfunc.getPage(HttpContext.Current.Request["page"]);
        int _pagesize = pgfunc.getPage(HttpContext.Current.Request["pagesize"]);
        int rsct, pgct;
        string _keyword = func.safeStr(Req("keyword"));
        string _st = func.safeStr(Req("st"));
        string _et = func.safeStr(Req("et"));
        string strWhere = "";
        if (!string.IsNullOrEmpty(_st)) strWhere = strWhere + " and tShsj >= '" + _st + "'";
        if (!string.IsNullOrEmpty(_et)) strWhere = strWhere + " and tShsj <= '" + _et + "'";
        string sql = pgfunc.GetSqlstr5(_page, _pagesize, "a.id,a.cFilename,case a.fFiletype when 0 then '文档' when 1 then '图片' when 2 then '视频' end as fFiletype,a.tDate,b.cMc,c.cUser", "[t_Filecollection]  a left join [t_Department] b on a.fDptid =  b.id left join [t_Users] c on c.id = a.fuid where fShjg = 1 and cFilename like '%" + _keyword + "%'" + strWhere, "order by a.id desc", out rsct, out pgct);
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Filename = dr["cFilename"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Filetype = dr["fFiletype"].ToString();
                inv.Mc = dr["cMc"].ToString();
                inv.User = dr["cUser"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("rsct", rsct);
        jo.Add("pgct", pgct);
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 批量下架
    /// </summary>
    protected string BatchNOSale()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if (_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _ids = func.safeStr(Req("ids"));
        if (!string.IsNullOrEmpty(_ids))
        {
            jo.Add("Result", 2);
            jo.Add("Message", "_ids参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        db.ExecNQ("update [t_Filecollection] set fIsonsale = 0 where id in(" + _ids + ")");
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    /// <summary>
    /// 发送公告
    /// </summary>
    protected string SendMessage()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if (_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _title = func.safeStr(Req("title"));
        if (!string.IsNullOrEmpty(_title))
        {
            jo.Add("Result", 2);
            jo.Add("Message", "title参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _content = func.safeStr(Req("content"));
        if (!string.IsNullOrEmpty(_content))
        {
            jo.Add("Result", 2);
            jo.Add("Message", "content参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        string _deptids = func.safeStr(Req("deptids"));
        if (!string.IsNullOrEmpty(_deptids))
        {
            jo.Add("Result", 2);
            jo.Add("Message", "deptids参数错误");
            return JsonConvert.SerializeObject(jo);
        }

        db.ExecNQ("insert into [t_Announcement] (cTitle,cContent,cDptids) values ('" + _title + "','" + _content + "','" + _deptids + "')");
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }


    /// <summary>
    /// 公告列表
    /// </summary>
    protected string MessageList()
    {
        JObject jo = new JObject();
        JArray ja = new JArray();
        string _uid = getUid();
        if (_uid == "0")
        {
            jo.Add("Result", 1);
            jo.Add("Message", "uid参数错误");
            return JsonConvert.SerializeObject(jo);
        }
        int _page = pgfunc.getPage(HttpContext.Current.Request["page"]);
        int _pagesize = pgfunc.getPage(HttpContext.Current.Request["pagesize"]);
        int rsct, pgct;
        string sql = pgfunc.GetSqlstr5(_page, _pagesize, "id,cTitle,cContent,tDate,cDptids","[t_Announcement] with(nolock) ", "order by id desc", out rsct, out pgct);
        using (IDataReader dr = db.dtReader(sql))
        {
            while (dr.Read())
            {
                dynamic inv = new JObject();
                inv.id = dr["id"].ToString();
                inv.Title = dr["cTitle"].ToString();
                inv.Date = dr["tDate"].ToString();
                inv.Content = dr["cContent"].ToString();
                inv.Dptids = dr["cDptids"].ToString();
                ja.Add(inv);
            }
        }
        jo.Add("rsct", rsct);
        jo.Add("pgct", pgct);
        jo.Add("List", ja);
        jo.Add("Result", 0);
        jo.Add("Message", "");
        return JsonConvert.SerializeObject(jo);
    }

    protected string getUid()
    {
        string _uid = getID(HttpUtility.UrlDecode(func.NoNull(Req("uid")).Replace("+", "%2b")));
        if (_uid == "0")
            _uid = pg.usid();
        return _uid;
    }

    /// <summary>
    /// 解密id
    /// </summary>
    protected string getID(string OpenID)
    {
        int id = 0;
        if (!string.IsNullOrEmpty(OpenID) && int.TryParse(new KeyPwd().Decrypto(OpenID), out id))
            return id.ToString();
        else
            return "0";
    }

    protected string Req(string s)
    {
        return HttpContext.Current.Request[s];
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}